<template>
    <h2 style="text-align: center;">病房信息展示</h2>
    <a href="/AddWard" class="btn btn-success">添加病房信息</a><br>
    病房名称:<input type="text" placeholder="请输入病房名称" v-model="name" style="margin-right: 10px;" />
    所属科室:<select v-model="Dept_Id" style="margin-right: 10px;">
                <option :value="-1">请选择</option>
                <option v-for="item in InfoDept" :value="item.Dept_Id">{{ item.Dept_Name }}</option>
            </select>
    <input type="button" value="查询" class="btn btn-success" @click="ShowWard"/>
    <table border="1" class="table table-bordered"> 
        <thead style="background-color: aquamarine;">
            <tr style="text-align: center;">
                <th>病房编号</th>
                <th>病房名称</th>
                <th>床位数</th>
                <th>空床位数</th>
                <th>病房状态</th>
                <th>所属科室</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody style="text-align: center;">
            <tr v-for="item in Info">
                <td>{{item.Dept_Id}}</td>
                <td>{{ item.Ward_Name }}</td>
                <td>{{ item.Ward_Num }}</td>
                <td>{{ item.Ward_SumNum }}</td>
                <td>{{ item.Ward_State?"启用":"维修" }}</td>
                <td>{{ item.Dept_Name }}</td>
                <td>
                    <a @click="UpdWard(item.Ward_Id)" class="btn btn-success">修改状态</a>
                </td>
            </tr>
        </tbody>
    </table>
    <span>
        数据共{{ PageInfo.zongtiao }}条，每页{{ PageInfo.size}}条，共{{ PageInfo.index }}/{{ PageInfo.zongpage }}页

        <a @click="FanYe('F')" class="btn btn-success" style="margin-right: 10px;">首页</a>
        <a @click="FanYe('U')" class="btn btn-success" style="margin-right: 10px;">上一页</a>
        <a @click="FanYe('N')" class="btn btn-success" style="margin-right: 10px;">下一页</a>
        <a @click="FanYe('E')" class="btn btn-success" style="margin-right: 10px;">尾页</a>
    </span>
</template>


<script setup lang="ts">
    import {ref,onMounted}from 'vue';
    import axios from 'axios';

    //显示响应式数据
    const Info=ref([{  
        Ward_Id: 0,
        Ward_Name: "",
        Ward_Num: "",
        Ward_SumNum: "",
        Ward_State: true,
        Dept_Id: 0,
        Dept_Name: ""
    }])
    //获取科室响应式数据
    const InfoDept=ref([{
        Dept_Id:0,
        Dept_Name:""
    }])
    //分页响应式数据
    const PageInfo=ref({
        index:1,
        size:2,
        zongtiao:0,
        zongpage:0
    })
    //分页
    const FanYe=(chooes:string)=>{
        switch(chooes)
        {
            case"F":
                PageInfo.value.index=1;
            break;
            case"U":
                if(PageInfo.value.index>1)
                {
                    PageInfo.value.index--;
                }
                else
                {
                    alert('当前已是第一页');
                    return;
                }
            break;
            case"N":
                if(PageInfo.value.index<PageInfo.value.zongpage)
                {
                    PageInfo.value.index++;
                }
                else
                {
                    alert('当前已是最后一页');
                    return;
                }
            break;
            case"E":
                PageInfo.value.index=PageInfo.value.zongpage;
            break;
        }
        ShowWard();//跳转到显示页面
    }
    //文档就绪函数
    onMounted(()=>{
        GetDepartments();//获取科室信息
        ShowWard();//显示
    })
    //获取科室信息
    const GetDepartments=()=>{
        axios.get("https://localhost:44306/api/Ward/GetDepartments")
        .then(res=>{
            InfoDept.value=res.data
        })
    }
    //用户查询
    const name=ref("");
    const Dept_Id=ref(-1);
    //显示
    const ShowWard=()=>{
        axios.get("https://localhost:44306/api/Ward/ShowWard",
            {
                params:{
                    name:name.value,
                    Dept_Id:Dept_Id.value,
                    index:PageInfo.value.index,
                    size:PageInfo.value.size
                }
            }
        )
        .then(res=>{
            Info.value=res.data.Plist,
            PageInfo.value.zongpage=res.data.ZongPage,
            PageInfo.value.zongtiao=res.data.ZongTiao
        })
    }

    //修改状态
    const UpdWard=(Ward_Id:number)=>{
        if(confirm("确定修改？")){
            axios.put("https://localhost:44306/api/Ward/UpdWard?id="+Ward_Id)
            .then(res=>{
                if(res.data>0)
                {
                    alert('修改成功');
                    ShowWard();//跳转到显示页面
                }
                else
                {
                    alert('修改失败');
                }
            })
        }
    }
</script>